/**
 * 
 */
package models;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import play.data.validation.MaxSize;
import play.data.validation.Required;
import play.db.jpa.Model;

/**
 * 
 * @author ThinkPad
 *
 */
@Entity
@Table(name="news")
public class News extends Model{

	@Required
	@MaxSize(1024)
	public String title;
	
	@Required
	@Lob
	public String content;
	
	@Column(name="create_time")
	public Date createTime = new Date();
	
	@Column(name="update_time")
	public Date updateTime = new Date();
	
	@ManyToOne
	public User creator = new User();
	
	@ManyToOne
	public User modifier = new User();
	
	@Override
	public void _save() {
		if(this.creator!=null){
			this.creator = User.findByUsername(this.creator.username);
		}
		if(this.modifier!=null){
			this.modifier = User.findByUsername(this.modifier.username);
		}
		this.updateTime = new Date();
		super._save();
	}
	
	@Override
	public String toString() {
		return this.title;
	}

	public static List<News> lastest(int size) {
		return find("from News order by createTime desc").fetch(size);
	}
	
	
	
}
